﻿using ChuanYe.Core.Utils;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{

    /// <summary>
    /// 辅助类
    /// </summary>
    public class ControllerHelper
    {
        /// <summary>
        /// MQTT Client
        /// </summary>
        public static MQClient client = new MQClient("ChuanYe_CFM_SM");

        /// <summary>
        /// 在线值守 MQTT {0}=UserId
        /// </summary>

        public static string UserOnlineTopic = "/CFM/UserOnline/{0}";

        /// <summary>
        /// 火灾消息MQTT  {0}=FireId,{1}=RecordType
        /// </summary>
        public static string FireMessageTopic = "/CFM/FireMessage/{0}/{1}";


        /// <summary>
        /// 火灾消息 {0}=AddressCounty {1}=AddressTown {2}=AddressVillage
        /// </summary>
        public static string FireAreaMessageTopic= "/CFM/FireAreaMessage/{0}/{1}/{2}";


        /// <summary>
        /// 任务及任务反馈 {0}=UserId
        /// </summary>
        public static string TaskMessageTopic = "/Task/{0}";

        /// <summary>
        /// 自定义消息 {0}=UserId
        /// </summary>
        public static string CustomMessageTopic = "/CustomMessage/{0}";



        /// <summary>
        /// 发送MQTT
        /// </summary>
        /// <param name="list"></param>
        public static  void SendMessage(List<FireMQTTResponseModel> list)
        {
            //进行MQTT消息推送
            foreach (var item in list)
            {
                var topic = string.Format(FireMessageTopic, item.FireId,item.RecordType);
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(item);
                client.Publish(topic, json, false);
            }
        }

        /// <summary>
        /// 发送MQTT
        /// </summary>
        /// <param name="list"></param>
        public static void SendAreaMessage(List<FireMQTTResponseModel> list)
        {
            //进行MQTT消息推送
            foreach (var item in list)
            {
                string topic = string.Format(FireAreaMessageTopic,item.AddressCounty,item.AddressTown,item.AddressVillage);
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(item);
                client.Publish(topic, json, false);
            }
        }

        /// <summary>
        /// 发送任务MQTT
        /// </summary>
        public static void SendTaskMessage(TaskMQTTResponseModel model) 
        {
            if (model != null)
            {
                var topic = string.Format(TaskMessageTopic, model.TargetUserId);
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(model);
                client.Publish(topic, json, false);
            }
        }
        /// <summary>
        /// 发送任务反馈MQTT
        /// </summary>
        public static void SendFeedbackMessage(FeedbackMQTTResponseModel model)
        {
            if (model != null)
            {
                var topic = string.Format(TaskMessageTopic, model.SendUserId);
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(model);
                client.Publish(topic, json, false);
            }
        }

        /// <summary>
        /// 发送自定义消息MQTT
        /// </summary>
        /// <param name="model">数据体</param>
        /// <param name="userId">目标用户</param>
        public static void SendCustomMessage(OtherMessageModel model,int userId)
        {
            if (model != null)
            {
                var topic = string.Format(CustomMessageTopic, userId);
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(model);
                client.Publish(topic, json, false);
            }
        }



    }


    /// <summary>
    /// 扩展类
    /// </summary>
    internal static class Extension
    {
        /// <summary>
        /// 获取客户Ip
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetClientIp(this HttpContext context)
        {
            var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                ip = context.Connection.RemoteIpAddress.ToString();
                if (ip == "::1")
                {
                    return "127.0.0.1";
                }
            }
            return ip;
        }
    }
}
